import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: happyiu
 * Date: 2021-11-15
 * Time: 20:57
 * Description:顺序表
 */
public class MyArrayList {

    public int [] elem;
    public int useSize;

    public MyArrayList(){
        int [] elem = new int[10];
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < useSize; i++) {
            System.out.println(elem[i]+" ");
        }
        System.out.println();
    }

    // 获取顺序表长度
    public int size() {
        return useSize;
    }



    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(pos < 0 || pos >useSize){
            System.out.println("pos位置不合法！");
            return;
        }
        if(isFull()){
            Arrays.copyOf(elem,elem.length * 2);
        }
        for (int i = this.useSize-1; i >= pos; i--) {
            elem[i+1]=elem[i];

        }
        this.elem[pos]=data;
        this.useSize++;

    }

    public boolean isFull(){
        return this.useSize == this.elem.length;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;

    }

    // 查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if(pos < 0 || pos >useSize){
            System.out.println("pos位置不合法！");
            return -1;

        }
        if(isEmpty()){
            System.out.println("顺序表为空！");
            return -1;
        }
        return elem[pos];

    }

    public boolean isEmpty(){
        return this.useSize==0;
    }

    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if(pos < 0 || pos >useSize){
            System.out.println("pos位置不合法！");
            return ;

        }
        if(isEmpty()){
            System.out.println("顺序表为空！");
            return ;
        }

        elem[pos] = value;

    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if(isEmpty()){
            System.out.println("顺序表为空");
            return;
        }
        int index = search(toRemove);
        if(index == -1) {
            System.out.println("没有你要删除的数字！");
            return;
        }
        for (int i = index; i < this.useSize-1; i++) {
            this.elem[i+1]=this.elem[i];
        }
        this.useSize--;

    }

    // 清空顺序表
    public void clear() {
        useSize =0;
    }



}
